//
//  VarietyHeaderView.m
//  PigMarket
//
//  Created by mac on 2019/4/8.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "VarietyHeaderView.h"

#define HeaderTitleColor [UIColor colorWithHexString:@"#666666"]
@implementation VarietyHeaderView
#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
        [self initAction];
        [self changeButtonStyle];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.defaultBtn];
    [self addSubview:self.weightBtn];
    [self addSubview:self.priceBtn];
    [self addSubview:self.dateBtn];
    [self addSubview:self.changeArrangeStyleBtn];
    [self addSubview:self.greenLine];
}

- (void)initLayout {
    // 布局子控件
    [self.priceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.changeArrangeStyleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.defaultBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.weightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).multipliedBy(0.5).offset(8);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.dateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).multipliedBy(1.5).offset(-8);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [self.greenLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(26, 1));
        make.centerX.mas_equalTo(self.defaultBtn.mas_centerX);
    }];
    
}
- (void)initAction {
    [self.defaultBtn addTarget:self action:@selector(defaultTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.weightBtn addTarget:self action:@selector(weightTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.priceBtn addTarget:self action:@selector(priceTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.dateBtn addTarget:self action:@selector(dateTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.changeArrangeStyleBtn addTarget:self action:@selector(changeArrangeStyleTapped:) forControlEvents:UIControlEventTouchUpInside];

}
#pragma mark - target action
//默认
- (void)defaultTapped:(UIButton *)button {
    [self changeSelectedButtonStyle:button];
    if (self.buttonClickBlock) {
        self.buttonClickBlock(ButtonTypeDefault,button);
    }
}
//体重
- (void)weightTapped:(UIButton *)button {
    [self changeSelectedButtonStyle:button];
    if (self.buttonClickBlock) {
        self.buttonClickBlock(ButtonTypeWeight,button);
    }
}
//价格
- (void)priceTapped:(UIButton *)button {
    [self changeSelectedButtonStyle:button];
    if (self.buttonClickBlock) {
        self.buttonClickBlock(ButtonTypePrice,button);
    }
}
//出栏时间
- (void)dateTapped:(UIButton *)button {
    [self changeSelectedButtonStyle:button];
    if (self.buttonClickBlock) {
        self.buttonClickBlock(ButtonTypeDate,button);
    }
}
//切换排列方式
- (void)changeArrangeStyleTapped:(UIButton *)button {
    button.selected = !button.selected;
    if (self.buttonClickBlock) {
        self.buttonClickBlock(ButtonTypeChangeArrange,button);
    }
}
#pragma mark - private method
//点击时，修改相应按钮的样式
- (void)changeSelectedButtonStyle:(UIButton *)button {
    [self.greenLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(button.mas_centerX);
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(26, 1));
    }];
    NSMutableArray *btnArr = [[NSMutableArray alloc] initWithObjects:self.defaultBtn,self.weightBtn,self.priceBtn,self.dateBtn, nil];
    for (int i = 0; i < 4; i++) {
        UIButton *btn = btnArr[i];
        if (button.tag == 100+i) {
            btn.selected = !button.selected;
            [btn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        }else {
            btn.selected = NO;
            [btn setTitleColor:HeaderTitleColor forState:UIControlStateNormal];
        }
    }
    
}
//修改按钮标题和图片的位置
- (void)changeButtonStyle {
    [self changeButtonStyle:self.weightBtn];
    [self changeButtonStyle:self.priceBtn];
    [self changeButtonStyle:self.dateBtn];
}
- (void)changeButtonStyle:(UIButton *)button {
    NSInteger titleWidth = button.titleLabel.intrinsicContentSize.width;
    NSInteger imgWidth = button.imageView.intrinsicContentSize.width;
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgWidth, 0, imgWidth)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth, 0, -titleWidth)];
    
}
#pragma mark - getter and setter
- (UIButton *)defaultBtn {
    if (!_defaultBtn) {
        _defaultBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_defaultBtn setTitle:@"默认" forState:UIControlStateNormal];
        [_defaultBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        _defaultBtn.titleLabel.font = [UIFont middleFont];
        _defaultBtn.tag = 100;
    }
    return _defaultBtn;
}
- (UIButton *)weightBtn {
    if (!_weightBtn) {
        _weightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_weightBtn setTitle:@"体重" forState:UIControlStateNormal];
        [_weightBtn setTitleColor:HeaderTitleColor forState:UIControlStateNormal];
        _weightBtn.titleLabel.font = [UIFont middleFont];
        [_weightBtn setImage:[UIImage imageNamed:@"adop_priceup"] forState:UIControlStateNormal];
        [_weightBtn setImage:[UIImage imageNamed:@"adop_pricedown"] forState:UIControlStateSelected];
        _weightBtn.tag = 101;
    }
    return _weightBtn;
}
- (UIButton *)priceBtn {
    if (!_priceBtn) {
        _priceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_priceBtn setTitle:@"价格" forState:UIControlStateNormal];
        [_priceBtn setTitleColor:HeaderTitleColor forState:UIControlStateNormal];
        _priceBtn.titleLabel.font = [UIFont middleFont];
        [_priceBtn setImage:[UIImage imageNamed:@"adop_priceup"] forState:UIControlStateNormal];
        [_priceBtn setImage:[UIImage imageNamed:@"adop_pricedown"] forState:UIControlStateSelected];
        _priceBtn.tag = 102;
    }
    return _priceBtn;
}
- (UIButton *)dateBtn {
    if (!_dateBtn) {
        _dateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dateBtn setTitle:@"出栏日期" forState:UIControlStateNormal];
        [_dateBtn setTitleColor:HeaderTitleColor forState:UIControlStateNormal];
        _dateBtn.titleLabel.font = [UIFont middleFont];
        [_dateBtn setImage:[UIImage imageNamed:@"adop_priceup"] forState:UIControlStateNormal];
        [_dateBtn setImage:[UIImage imageNamed:@"adop_pricedown"] forState:UIControlStateSelected];
        _dateBtn.tag = 103;
    }
    return _dateBtn;
}
- (UIButton *)changeArrangeStyleBtn {
    if (!_changeArrangeStyleBtn) {
        _changeArrangeStyleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_changeArrangeStyleBtn setTitleColor:HeaderTitleColor forState:UIControlStateNormal];
        _changeArrangeStyleBtn.titleLabel.font = [UIFont middleFont];
        [_changeArrangeStyleBtn setBackgroundImage:[UIImage imageNamed:@"adop_adoplist_default"] forState:UIControlStateNormal];
        [_changeArrangeStyleBtn setBackgroundImage:[UIImage imageNamed:@"adop_adoplist_selected"] forState:UIControlStateSelected];
    }
    return _changeArrangeStyleBtn;
}
- (UIView *)greenLine {
    if (!_greenLine) {
        _greenLine = [[UIView alloc] init];
        _greenLine.backgroundColor = [UIColor primaryColor];
    }
    return _greenLine;
}
@end
